﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace LongNet.Util
{
    public class PublicClass
    {
        public static string Strdigital = "零壹贰叁肆伍陆柒捌玖";

        private string _connstr;
        /// <summary>
        /// 开发办超级管理员不作限制
        /// </summary>
        public static int developmentUid = 49;
        public string connstr
        {
            get { return _connstr; }
            set { _connstr = value; }
        }

        public static string GetConnStr()
        {
            return ConfigurationSettings.AppSettings["connSqlString"];
        }
        public static Object GetNullValue(object objValue)
        {
            if (objValue == null)
            {
                return DBNull.Value;
            }
            else if (objValue.GetType().Equals((new DateTime()).GetType()))
            {
                if (Convert.ToDateTime(objValue).Year < 1957)  //Sql最小支持年份
                {
                    return DBNull.Value;
                }
                else
                {
                    return objValue;
                }
            }
            else
            {
                return objValue;
            }
        }
        /// <summary>
        /// 判断一个字符串是否为数字
        /// </summary>
        /// <param name="strSource">需要判断的字符串</param>
        /// <returns></returns>
        public static bool IsNumberic(string strSource)
        {
            System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"\d+\.?\d*|\.\d+");

            if (rex.IsMatch(strSource))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断一个字符串是否为整数
        /// </summary>
        /// <param name="strMessage">需要判断的字符串</param>
        /// <returns></returns>
        public static bool IsIntNumberic(string strMessage)
        {
            System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"^\d+$");

            if (rex.IsMatch(strMessage))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取double类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static double GetDoubleValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return Convert.ToDouble(objValue);
                }
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取 DateTime 类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static DateTime GetDateTimeValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return Convert.ToDateTime(objValue);
                }
            }
            catch
            {
                return DateTime.MinValue;
            }
        }
        public static string DBDateTimeToShortString(object DBValue)
        {
            try
            {
                if (DBValue == Convert.DBNull)
                {
                    return "";
                }
                else
                {
                    return Convert.ToDateTime(DBValue).ToShortDateString();
                }
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBValue"></param>
        /// <returns></returns>
        public static string DBDateTimeYear(object DBValue)
        {
            try
            {
                if (DBValue == Convert.DBNull)
                {
                    return "";
                }
                else
                {
                    return Convert.ToDateTime(DBValue).Year.ToString();
                }
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 获取Int32类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static System.Int32 GetInt32Value(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(objValue);
                }
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取Int32类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static int GetIntValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return int.Parse(objValue.ToString());
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取Float类型的值,默认二位小数点。
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static float GetFloatValue(object objValue)
        {
            return GetFloatValue(objValue, "F2");
        }

        /// <summary>
        /// 获取Float类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <param name="digitsStr">小数点格式，如F2,二位小数</param>
        /// <returns></returns>
        public static float GetFloatValue(object objValue, string digitsStr)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return float.Parse(float.Parse(objValue.ToString()).ToString(digitsStr));
                }
            }
            catch
            {
                return 0;
            }
        }
        public static List<string> GetSessionRolesToList(string RoleNames)
        {
            List<string> list = new List<string>();
            if (RoleNames != "")
            {
                string[] x = RoleNames.Split(',');
                for (int i = 0; i < x.GetLength(0); i++)
                {
                    list.Add(x[i]);
                }

            }
            return list;
        }

        /// <summary>
        /// 根据信息类别返回枚举列表
        /// </summary>
        /// <param name="infoType">信息类别</param>
        /// <returns></returns>
        public static DataView GetEnum(string infoType)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/app_data/enum.xml"));
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = "infoType=" + infoType;
            return dv;
        }
        /// <summary>
        /// 根据信息类别与枚举值返回枚举文本
        /// </summary>
        /// <param name="infoType">信息类别</param>
        /// <param name="strValue">文本值</param>
        /// <returns></returns>
        public static string GetTextByValue(string infoType, string strValue)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/app_data/enum.xml"));
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = "infoType='" + infoType + "'";
            for (int i = 0; i < dv.Count; i++)
            {
                if (strValue == dv[i]["value"].ToString())
                {
                    return dv[i]["text"].ToString();
                }
            }
            return "";
        }
        public static void AlertMessage(string strMsg, bool clickBack)
        {
            if (clickBack)
            {
                System.Web.HttpContext.Current.Response.Write("<Script language='Javascript'>alert('" + strMsg + "');history.back();</script>");
            }
            else
            {
                System.Web.HttpContext.Current.Response.Write("<Script language='Javascript'>alert('" + strMsg + "');</script>");
            }

        }
        public static bool IsNew()
        {
            if (HttpContext.Current.Request.QueryString["Id"] == null || HttpContext.Current.Request.QueryString["Id"] == "")
            {
                return true;
            }
            else
            {
                return false;

            }
        }
        /// <summary>
        /// 生成随机密码
        /// </summary>
        /// <param name="pwdchars">密码可取的字符</param>
        /// <param name="passwordLength">密码长度</param>
        /// <returns></returns>
        public static string MakeRandPassword(string pwdchars, int passwordLength)
        {
            string tmpstr = "";

            int iRandNum;

            if (pwdchars == "")
            {
                pwdchars = "abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            }

            Random rnd = new Random();

            for (int i = 0; i < passwordLength; i++)
            {
                iRandNum = rnd.Next(pwdchars.Length);

                tmpstr += pwdchars[iRandNum];
            }
            return tmpstr;
        }
        /// <summary>
        /// 根据起始日期和工作日天数取得结束日期
        /// </summary>
        /// <param name="startDate">起始日期</param>
        /// <param name="dayNumber">工作日天数</param>
        /// <returns></returns>
        public static DateTime GetWorkEndDateByDayNumAndStartDate(DateTime startDate, int dayNumber)
        {
            DateTime endDate = startDate;

            for (int i = 1; i <= dayNumber; i++)
            {
                endDate = endDate.AddDays(1);
                if (endDate.DayOfWeek == DayOfWeek.Saturday)
                {
                    endDate = endDate.AddDays(1);
                }
                if (endDate.DayOfWeek == DayOfWeek.Sunday)
                {
                    endDate = endDate.AddDays(1);
                }
            }
            return endDate;
        }
        /// <summary>
        /// 返回格式化的货币用于输入__仟__佰__拾__万__仟__佰__拾__,0为初始化
        /// </summary>
        /// <param name="controlID"></param>
        /// <returns></returns>
        public static string GetCurrency(string controlID, int money, string css)
        {
            if (money == 0)
            {
                string m = "";
                string s = " <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_1'  value='" + m + "' />仟" +
                            "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='1'   name='" + controlID + "_2' value='" + m + "' />佰" +
                            "<input type='text' id='" + controlID + "_3' class='txtD " + css + "'   maxlength ='1' name='" + controlID + "_3'  value='" + m + "' />拾" +
                            "<input type='text' id='" + controlID + "_4'  class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_4'  value='" + m + "' />万" +
                            "<input type='text' id='" + controlID + "_5'  class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_5' value='" + m + "' />仟" +
                            "<input type='text' id='" + controlID + "_6'   class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_6'  value='" + m + "' />佰" +
                            "<input type='text' id='" + controlID + "_7'  class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_7'  value='" + m + "' />拾" +
                            "<input type='text' id='" + controlID + "_8'  class='txtD " + css + "'  maxlength ='1' name='" + controlID + "_8'  value='" + m + "' />";
                return s;
            }
            else
            {
                string s = " <input type='text' id='" + controlID + "_1'  class='txtD " + css + "'   maxlength ='1'  name='" + controlID + "_1'  value='" + GetIntStr(money, 8) + "' />仟" +
                            "<input type='text' id='" + controlID + "_2'  class='txtD " + css + "'  maxlength ='1' name='" + controlID + "_2'  value='" + GetIntStr(money, 7) + "' />佰" +
                            "<input type='text' id='" + controlID + "_3'  class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_3'  value='" + GetIntStr(money, 6) + "' />拾" +
                            "<input type='text' id='" + controlID + "_4'   class='txtD " + css + "' maxlength ='1'   name='" + controlID + "_4'  value='" + GetIntStr(money, 5) + "' />万" +
                            "<input type='text' id='" + controlID + "_5'   class='txtD " + css + "'  maxlength ='1' name='" + controlID + "_5'   value='" + GetIntStr(money, 4) + "' />仟" +
                            "<input type='text' id='" + controlID + "_6'  class='txtD " + css + "'   maxlength ='1' name='" + controlID + "_6' value='" + GetIntStr(money, 3) + "' />佰" +
                            "<input type='text' id='" + controlID + "_7'   class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_7' value='" + GetIntStr(money, 2) + "' />拾" +
                            "<input type='text' id='" + controlID + "_8'  class='txtD " + css + "'   maxlength ='1' name='" + controlID + "_8'  value='" + GetIntStr(money, 1) + "' />";

                return s;
            }
        }




        static string GetIntStr(int money, int p)
        {
            string[] s = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string m = money.ToString();
            if (m.Length >= p)
            {

                int pos = Convert.ToInt32(m[m.Length - p].ToString());
                return s[pos];

            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 返回格式化的货币用于输入__仟__佰__拾__万__仟__佰__拾__,0为初始化
        /// </summary>
        /// <param name="controlID"></param>
        /// <returns></returns>
        public static string GetCurrency(string controlID, int money, string defaultvalue, string css)
        {

            string s = " <input type='text' id='" + controlID + "_1'  class='txtD " + css + "'   maxlength ='1'  name='" + controlID + "_1'  value='" + GetIntStr(money, 8, defaultvalue) + "' />仟" +
                        "<input type='text' id='" + controlID + "_2'  class='txtD " + css + "'  maxlength ='1' name='" + controlID + "_2'  value='" + GetIntStr(money, 7, defaultvalue) + "' />佰" +
                        "<input type='text' id='" + controlID + "_3'  class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_3'  value='" + GetIntStr(money, 6, defaultvalue) + "' />拾" +
                        "<input type='text' id='" + controlID + "_4'   class='txtD " + css + "' maxlength ='1'   name='" + controlID + "_4'  value='" + GetIntStr(money, 5, defaultvalue) + "' />万" +
                        "<input type='text' id='" + controlID + "_5'   class='txtD " + css + "'  maxlength ='1' name='" + controlID + "_5'   value='" + GetIntStr(money, 4, defaultvalue) + "' />仟" +
                        "<input type='text' id='" + controlID + "_6'  class='txtD " + css + "'   maxlength ='1' name='" + controlID + "_6' value='" + GetIntStr(money, 3, defaultvalue) + "' />佰" +
                        "<input type='text' id='" + controlID + "_7'   class='txtD " + css + "'  maxlength ='1'  name='" + controlID + "_7' value='" + GetIntStr(money, 2, defaultvalue) + "' />拾" +
                        "<input type='text' id='" + controlID + "_8'  class='txtD " + css + "'   maxlength ='1' name='" + controlID + "_8'  value='" + GetIntStr(money, 1, defaultvalue) + "' />";
            return s;

        }
        static string GetIntStr(int money, int p, string defaultvalue)
        {
            string[] s = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string m = money.ToString();
            if (m.Length >= p)
            {

                int pos = Convert.ToInt32(m[m.Length - p].ToString());
                return s[pos];
            }
            else
            {
                return defaultvalue;
            }
        }
        /// <summary>
        /// 返回金额
        /// </summary>
        /// <param name="list">{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}</param>
        /// <returns></returns>
        public static int ConvertMoney(string[] list)
        {
            int total = 0;
            int l = list.Length;
            for (int i = 0; i < list.Length; i++)
            {
                total += Convert.ToInt32(GetDigital(list[i].ToString()) * System.Math.Pow(10, (l - i - 1.0)));
            }
            return total;
        }
        /// <summary>
        /// 通过大写返回对应的数字
        /// </summary>
        /// <param name="chr"></param>
        /// <returns></returns>
        public static int GetDigital(string chr)
        {
            string[] s = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            int[] d = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == chr)
                {
                    return d[i];
                }
            }
            return 0;
        }
        /// <summary>
        /// 返回日期2009-3-3
        /// </summary>
        /// <param name="controlID"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDateTime(string controlID, DateTime dt, string css)
        {
            if (dt == null)
            {
                string s = "  <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1'  value='' />年" +
                             "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2'  value='' />月" +
                             "<input type='text' id='" + controlID + "_3' class='txtD " + css + "'  maxlength ='2'  name='" + controlID + "_3'  value='' />日";
                return s;
            }
            else
            {
                string s = " <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1' value='" + dt.Year.ToString() + "' />年" +
                            "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2' value='" + dt.Month.ToString() + "' />月" +
                            "<input type='text' id='" + controlID + "_3' class='txtD " + css + "' maxlength ='2' name='" + controlID + "_3' value='" + dt.Day.ToString() + "' />日";
                return s;
            }
        }
        /// <summary>
        /// 返回格式转换后的时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDateTime(DateTime dt)
        {
            if (dt != null)
            {
                return dt.Year.ToString() + "年" + dt.Month.ToString() + "月" + dt.Day.ToString() + "日";
            }
            return "";

        }
        /// <summary>
        /// 日期
        /// </summary>
        /// <param name="controlID"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDateTime(string controlID, string dt, string css)
        {
            if (dt == null)
            {
                string s = "  <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1'  value='' />年" +
                             "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2'  value='' />月" +
                             "<input type='text' id='" + controlID + "_3' class='txtD " + css + "'  maxlength ='2'  name='" + controlID + "_3'  value='' />日";
                return s;
            }
            else
            {
                try
                {
                    DateTime dtInput = Convert.ToDateTime(dt);
                    string s = " <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1' value='" + dtInput.Year.ToString() + "' />年" +
                                "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2' value='" + dtInput.Month.ToString() + "' />月" +
                                "<input type='text' id='" + controlID + "_3' class='txtD " + css + "' maxlength ='2' name='" + controlID + "_3' value='" + dtInput.Day.ToString() + "' />日";
                    return s;
                }
                catch
                {
                    string s = "  <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1'  value='' />年" +
                            "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2'  value='' />月" +
                            "<input type='text' id='" + controlID + "_3' class='txtD " + css + "'  maxlength ='2'  name='" + controlID + "_3'  value='' />日";
                    return s;
                }
            }
        }
        /// <summary>
        /// 日期
        /// </summary>
        /// <param name="controlID"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDateTime(string controlID, string dt, string defaultValue, string css)
        {
            if (dt == null || dt.Trim() == "")
            {
                string s = "  <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1'  value='" + defaultValue + "' />年" +
                             "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2'  value='" + defaultValue + "' />月" +
                             "<input type='text' id='" + controlID + "_3' class='txtD " + css + "'  maxlength ='2'  name='" + controlID + "_3'  value='" + defaultValue + "' />日";
                return s;
            }
            else
            {
                try
                {
                    DateTime dtInput = Convert.ToDateTime(dt);
                    string s = " <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1' value='" + dtInput.Year.ToString() + "' />年" +
                                "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2' value='" + dtInput.Month.ToString() + "' />月" +
                                "<input type='text' id='" + controlID + "_3' class='txtD " + css + "' maxlength ='2' name='" + controlID + "_3' value='" + dtInput.Day.ToString() + "' />日";
                    return s;
                }
                catch
                {
                    string s = "  <input type='text' id='" + controlID + "_1' class='txtD " + css + "'  maxlength ='4' name='" + controlID + "_1'  value='' />年" +
                            "<input type='text' id='" + controlID + "_2' class='txtD " + css + "'  maxlength ='2' name='" + controlID + "_2'  value='' />月" +
                            "<input type='text' id='" + controlID + "_3' class='txtD " + css + "'  maxlength ='2'  name='" + controlID + "_3'  value='' />日";
                    return s;
                }

            }
        }
        public static DateTime ConvertDatetime(string[] dt)
        {
            return Convert.ToDateTime(dt[0] + "-" + dt[1] + "-" + dt[2]);
        }
        /// <summary>
        /// 获取Xml配置中的基础信息键值对
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetCategoryFromXml(string Key)
        {
            return GetCategoryFromXml(Key, "");
        }
        /// <summary>
        /// 获取Xml配置中的基础信息键值对
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetCategoryFromXml(string Key, string Filter)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

                xmlDoc.Load(InitInfo.XmlPath);

                System.Xml.XmlNodeList xnodlist = xmlDoc.SelectNodes("combos");

                System.Xml.XmlNode xmlNode = xnodlist[0].SelectSingleNode("combo[@id=\"" + Key + "\"]");

                if (xmlNode.Attributes["type"].Value == "TABLE")
                {
                    SqlConnection sqlCon = null;
                    try
                    {
                        sqlCon = new SqlConnection(SqlHelper.ConnectionString);

                        string strSql = "Select " + xmlNode.Attributes["keyColumn"].Value + "," + xmlNode.Attributes["column"].Value + " From " + xmlNode.Attributes["table"].Value + " " + Filter;

                        DataSet ds = SqlHelper.ExecuteDataset(sqlCon, CommandType.Text, strSql);
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            dic.Add(dr[xmlNode.Attributes["keyColumn"].Value].ToString(), dr[xmlNode.Attributes["column"].Value].ToString());
                        }
                    }
                    catch (Exception exConn)
                    {
                        sqlCon.Close();
                        throw exConn;
                    }
                    finally
                    {
                        if (sqlCon.State == ConnectionState.Open) sqlCon.Close();
                        sqlCon.Dispose();
                    }

                }
                if (xmlNode.Attributes["type"].Value == "CODE")
                {
                    xnodlist = xmlNode.ChildNodes;

                    foreach (System.Xml.XmlNode childNode in xmlNode.ChildNodes)
                    {
                        if (("," + Filter.Trim() + ",").IndexOf(childNode.Attributes["value"].Value) < 0)
                        {
                            dic.Add(childNode.Attributes["value"].Value, childNode.InnerText);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
            return dic;
        }
        /// <summary>
        /// 根据基本基本配置表中的Code、Value，获取基本配置表中的Text
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="strKeyValue"></param>
        /// <returns></returns>
        public static string GetTextFormTableByKey(string strCode, string strKeyValue)
        {
            return GetValueFormXmlByKey("CategorySettings", strKeyValue, " Where CategoryTypeCode = '" + strCode + "'");
        }

        /// <summary>
        /// 根据XML中的Code、Value，获取基本配置表中的Text
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="strKeyValue"></param>
        /// <returns></returns>
        public static string GetValueFormXmlByKey(string strKey, string strKeyValue)
        {
            return GetValueFormXmlByKey(strKey, strKeyValue, "");
        }

        /// <summary>
        /// （带条件，条件里面须加上Where关键字）根据XML中的Code、Value，获取基本配置表中的Text
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="strKeyValue"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static string GetValueFormXmlByKey(string strKey, string strKeyValue, string filter)
        {
            try
            {
                Dictionary<string, string> m_Dictionary = GetCategoryFromXml(strKey, filter);

                return GetValueByKey(m_Dictionary, strKeyValue);
            }
            catch (Exception e)
            {
                return "";
            }
        }
        /// <summary>
        /// 根据Key从Dictionary对象获取值
        /// </summary>
        /// <param name="m_Dictionary">Dictionary</param>
        /// <param name="Key">Key</param>
        /// <returns></returns>
        public static string GetValueByKey(Dictionary<string, string> m_Dictionary, string Key)
        {
            string strValue = "";

            if (m_Dictionary.TryGetValue(Key, out strValue))
            {
                return strValue;
            }
            else
            {
                return "";
            }
        }
        public static U GetValueByKey<T, U>(Dictionary<T, U> m_Dictionary, T Key)
        {
            U UValue = default(U);

            if (m_Dictionary.TryGetValue(Key, out UValue))
            {
                return UValue;
            }
            else
            {
                return UValue;
            }
        }
        /// <summary>
        /// 根据Text值获取Value值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="m_Dictionary"></param>
        /// <param name="UText"></param>
        /// <returns></returns>
        public static T GetValueByText<T, U>(Dictionary<T, U> m_Dictionary, U UText)
        {
            foreach (KeyValuePair<T, U> ss in m_Dictionary)
            {
                try
                {
                    if (ss.Value.Equals(UText))
                    {
                        return ss.Key;
                    }

                }
                catch
                {
                    return default(T);
                }
            }
            return default(T);
        }
        /// <summary>
        /// 根据Text返回Value
        /// </summary>
        /// <param name="m_Dictionary"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static int GetValueByText(Dictionary<string, string> m_Dictionary, string text)
        {
            foreach (KeyValuePair<string, string> ss in m_Dictionary)
            {
                try
                {
                    if (ss.Value == text)
                        return Convert.ToInt32(ss.Key);
                }
                catch
                {
                    return 0;
                }
            }
            return 0;
        }





        /// <summary>
        /// 复制一个对象的内容到另外一个对象复本
        /// </summary>
        /// <param name="objSource">源对象</param>
        /// <param name="objTarget">目标对象</param>
        /// <returns></returns>
        public static bool CopyProperties(object objSource, object objTarget)
        {
            try
            {
                          
                PropertyInfo[] m_PropertyInfoArray = objSource.GetType().GetProperties();           
                for (int i = 0; i < m_PropertyInfoArray.GetLength(0); i++)
                {
                    if (objTarget.GetType().GetProperty(m_PropertyInfoArray[i].Name)!=null)
                   {
                    object objValue = m_PropertyInfoArray[i].GetValue(objSource, null);

                    objTarget.GetType().GetProperty(m_PropertyInfoArray[i].Name).SetValue(objTarget, Convert.ChangeType(objValue, m_PropertyInfoArray[i].PropertyType), null);
                   }
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
      

        /// <summary>
        /// 复制一个对象的内容到另外一个对象复本
        /// </summary>
        /// <param name="objSource">源对象</param>
        /// <param name="objTarget">目标对象</param>
        /// <returns></returns>
        public static bool CopyObject(object objSource, object objTarget)
        {
            try
            {
                if (objTarget == null)
                {
                    objTarget = Convert.ChangeType(objSource, objSource.GetType());

                    return true;
                }
                if (!objSource.GetType().Equals(objSource.GetType()))
                {
                    throw new Exception("源对象和目标对象类型不匹配，无法复制！");
                }
                PropertyInfo[] m_PropertyInfoArray = objSource.GetType().GetProperties();

                for (int i = 0; i < m_PropertyInfoArray.GetLength(0); i++)
                {
                    object objValue = m_PropertyInfoArray[i].GetValue(objSource, null);

                    objTarget.GetType().GetProperty(m_PropertyInfoArray[i].Name).SetValue(objTarget, Convert.ChangeType(objValue, m_PropertyInfoArray[i].PropertyType), null);
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 复制数据行到Model对象方法
        /// </summary>
        /// <typeparam name="T">要赋值的数据对象泛型</typeparam>
        /// <param name="dataRow">数据行</param>
        /// <param name="objModel">要赋值的数据对象</param>
        /// <returns></returns>
        public static bool CopyDataRowToModel<T>(DataRow dataRow, ref T objModel)
        {
            try
            {
                if (dataRow == null)
                {
                    objModel = default(T);

                    return true;
                }
                System.Reflection.PropertyInfo[] m_PropertyInfos = objModel.GetType().GetProperties();

                string strPropertyName = "";

                for (int i = 0; i < m_PropertyInfos.GetLength(0); i++)
                {
                    strPropertyName = m_PropertyInfos[i].Name;

                    if (dataRow.Table.Columns.Contains(strPropertyName) && !Convert.IsDBNull(dataRow[strPropertyName]))
                    {
                        m_PropertyInfos[i].SetValue(objModel, Convert.ChangeType(dataRow[strPropertyName], m_PropertyInfos[i].PropertyType), null);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 复制Model对象到数据行方法
        /// </summary>
        /// <typeparam name="T">源数据对象泛型</typeparam>
        /// <param name="objModel">源数据对象</param>
        /// <param name="dataRow">要赋值的数据行</param>
        /// <returns></returns>
        public static bool CopyModelToDataRow<T>(ref T objModel, DataRow dataRow)
        {
            try
            {
                if (dataRow == null)
                {
                    objModel = default(T);

                    return true;
                }
                System.Reflection.PropertyInfo[] m_PropertyInfos = objModel.GetType().GetProperties();

                string strPropertyName = "";

                for (int i = 0; i < m_PropertyInfos.GetLength(0); i++)
                {
                    strPropertyName = m_PropertyInfos[i].Name;

                    if (dataRow.Table.Columns.Contains(strPropertyName) && m_PropertyInfos[i].GetValue(objModel, null) != null)
                    {
                        dataRow[strPropertyName] = m_PropertyInfos[i].GetValue(objModel, null);

                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 将一个数组转换成Int32的数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceArray"></param>
        /// <returns></returns>
        public static int[] ChangeToIntArray<T>(T[] sourceArray)
        {
            try
            {
                int[] m_TargetArray = new int[sourceArray.GetLength(0)];

                for (int i = 0; i < m_TargetArray.GetLength(0); i++)
                {
                    m_TargetArray[i] = Convert.ToInt32(sourceArray[i]);
                }
                return m_TargetArray;
            }
            catch
            {
                throw new Exception("<" + sourceArray.GetType().FullName + ">类型无法转换成<System.Int32>[]类型！");
            }
        }

        public static string AddString(string sourceString, string separator)
        {
            return string.Concat(separator, sourceString, separator);
        }
        /// <summary>
        /// 将一个数组转换成String类型的数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceArray"></param>
        /// <returns></returns>
        public static string[] ChangeToStringArray<T>(T[] sourceArray)
        {
            try
            {
                string[] m_TargetArray = new string[sourceArray.GetLength(0)];

                for (int i = 0; i < m_TargetArray.GetLength(0); i++)
                {
                    m_TargetArray[i] = Convert.ToString(sourceArray[i]);
                }
                return m_TargetArray;
            }
            catch
            {
                throw new Exception("<" + sourceArray.GetType().FullName + ">类型无法转换成<System.String>[]类型！");
            }
        }
        /// <summary>
        /// 遍历某个TreeNode及该节点下的所有节点
        /// </summary>
        /// <param name="pNode">TreeView中的某个节点</param>
        /// <param name="m_TreeNodes">需要返回的节点集合</param>
        public static void RansackTreeNode(TreeNode pNode, DataTable dataTable)
        {
            if (dataTable.Select("Id=" + pNode.Value).GetLength(0) > 0)
            {
                pNode.Checked = true;
            }
            for (int i = 0; i < pNode.ChildNodes.Count; i++)
            {
                RansackTreeNode(pNode.ChildNodes[i], dataTable);
            }
        }
        /// <summary>
        /// 遍历某个TreeView下的所有节点
        /// </summary>
        /// <param name="pNode">TreeView中的某个节点</param>
        /// <param name="m_TreeNodes">需要返回的节点集合</param>
        public static void RansackTreeNode(TreeView m_TreeView, DataTable dataTable)
        {
            foreach (TreeNode m_TreeNode in m_TreeView.Nodes)
            {
                RansackTreeNode(m_TreeNode, dataTable);
            }
        }


        /// <summary>
        /// 根据字符数组返回一个字典
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Dictionary<int, int> GetDicByStringArray(string[] str)
        {
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < str.Length; i++)
            {
                dic.Add(Util.PublicClass.GetInt32Value(str[i]), i);
            }

            return dic;
        }

        /// <summary>
        /// 根据业务宗号获得条形码
        /// </summary>
        /// <param name="OperationNumber">业务宗号</param>
        /// <returns></returns>
        public static string GetBarCode(string OperationNumber)
        {
            int checkcode_input = -1;//输入的校验码
            if (!Regex.IsMatch(OperationNumber, @"^\d{12}$"))
            {
                if (!Regex.IsMatch(OperationNumber, @"^\d{13}$"))
                {
                    return "0";
                    throw new Exception("存在不允许的字符!");
                }
                else
                {
                    checkcode_input = int.Parse(OperationNumber[12].ToString());
                    OperationNumber = OperationNumber.Substring(0, 12);
                }
            }

            int sum_even = 0;//偶数位之和
            int sum_odd = 0;//奇数位之和

            for (int i = 0; i < 12; i++)
            {
                if (i % 2 == 0)
                {
                    sum_odd += int.Parse(OperationNumber[i].ToString());
                }
                else
                {
                    sum_even += int.Parse(OperationNumber[i].ToString());
                }
            }

            int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码

            if (checkcode_input > 0 && checkcode_input != checkcode)
            {
                return "0";
                throw new Exception("输入的校验码错误!");
            }

            OperationNumber += checkcode;//变成13位
            return OperationNumber;

        }

        /// <summary>
        /// 根据业务宗号获取条形码图片
        /// </summary>
        /// <param name="OperationNumber"></param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static string GetBarCodeImage(string OperationNumber, int width, int height)
        {
            int checkcode_input = -1;//输入的校验码
            if (!Regex.IsMatch(OperationNumber, @"^\d{12}$"))
            {
                if (!Regex.IsMatch(OperationNumber, @"^\d{13}$"))
                {
                    return "存在不允许的字符！";
                }
                else
                {
                    checkcode_input = int.Parse(OperationNumber[12].ToString());
                    OperationNumber = OperationNumber.Substring(0, 12);
                }
            }

            int sum_even = 0;//偶数位之和
            int sum_odd = 0;//奇数位之和

            for (int i = 0; i < 12; i++)
            {
                if (i % 2 == 0)
                {
                    sum_odd += int.Parse(OperationNumber[i].ToString());
                }
                else
                {
                    sum_even += int.Parse(OperationNumber[i].ToString());
                }
            }

            int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码

            if (checkcode_input > 0 && checkcode_input != checkcode)
            {
                return "输入的校验码错误！";
            }

            OperationNumber += checkcode;//变成13位

            // 000000000101左侧42个01010右侧35个校验7个101000000000
            // 6        101左侧6位 01010右侧5位 校验1位101000000000

            string result_bin = "";//二进制串
            result_bin += "000000000101";

            string type = ean13type(OperationNumber[0]);
            for (int i = 1; i < 7; i++)
            {
                result_bin += ean13(OperationNumber[i], type[i - 1]);
            }
            result_bin += "01010";
            for (int i = 7; i < 13; i++)
            {
                result_bin += ean13(OperationNumber[i], 'C');
            }
            result_bin += "101000000000";

            string result_html = "";//HTML代码
            string color = "";//颜色
            int height_bottom = width * 5;
            foreach (char c in result_bin)
            {
                color = c == '0' ? "#FFFFFF" : "#000000";
                result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
            }
            result_html += "<div style=\"clear:both\"></div>";

            result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;text-align:center;\">" + OperationNumber[0] + "</div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            for (int i = 1; i < 7; i++)
            {
                result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + OperationNumber[i] + "</div>";
            }
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            for (int i = 7; i < 13; i++)
            {
                result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + OperationNumber[i] + "</div>";
            }
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;\"></div>";
            result_html += "<div style=\"clear:both\"></div>";

            return "<div style=\"background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + result_html + "</div>";
        }
        private static string ean13(char c, char type)
        {
            switch (type)
            {
                case 'A':
                    {
                        switch (c)
                        {
                            case '0': return "0001101";
                            case '1': return "0011001";
                            case '2': return "0010011";
                            case '3': return "0111101";//011101
                            case '4': return "0100011";
                            case '5': return "0110001";
                            case '6': return "0101111";
                            case '7': return "0111011";
                            case '8': return "0110111";
                            case '9': return "0001011";
                            default: return "Error!";
                        }
                    }
                case 'B':
                    {
                        switch (c)
                        {
                            case '0': return "0100111";
                            case '1': return "0110011";
                            case '2': return "0011011";
                            case '3': return "0100001";
                            case '4': return "0011101";
                            case '5': return "0111001";
                            case '6': return "0000101";//000101
                            case '7': return "0010001";
                            case '8': return "0001001";
                            case '9': return "0010111";
                            default: return "Error!";
                        }
                    }
                case 'C':
                    {
                        switch (c)
                        {
                            case '0': return "1110010";
                            case '1': return "1100110";
                            case '2': return "1101100";
                            case '3': return "1000010";
                            case '4': return "1011100";
                            case '5': return "1001110";
                            case '6': return "1010000";
                            case '7': return "1000100";
                            case '8': return "1001000";
                            case '9': return "1110100";
                            default: return "Error!";
                        }
                    }
                default: return "Error!";
            }
        }
        private static string ean13type(char c)
        {
            switch (c)
            {
                case '0': return "AAAAAA";
                case '1': return "AABABB";
                case '2': return "AABBAB";
                case '3': return "AABBBA";
                case '4': return "ABAABB";
                case '5': return "ABBAAB";
                case '6': return "ABBBAA";//中国
                case '7': return "ABABAB";
                case '8': return "ABABBA";
                case '9': return "ABBABA";
                default: return "Error!";
            }
        }

        /// <summary>
        /// 模糊查询不带逗号的信息
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public static string CharIndex(string keyword, string fieldname)
        {
            return string.Format(" charindex('{0}',{1})>0 ", keyword.Replace("'", "''"), fieldname.Replace("'", "''"));
        }


        /// <summary>
        /// 模糊查询Ids记录
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="fieldname"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        public static string CharIndexIds(string keyword, string fieldname)
        {
            return CharIndexIds(keyword, fieldname, ",");
        }

        /// <summary>
        /// 模糊查询Ids记录
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="fieldname"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        public static string CharIndexIds(string keyword, string fieldname, string splitStr)
        {
            string[] keywords = keyword.Split(splitStr.ToCharArray());

            if (keywords == null && keywords.Length == 0) return "";
            string charIndexStr = "(";

            for (int i = 0; i < keywords.Length; i++)
            {
                charIndexStr += string.Format(" charindex(',{0},', ','+{1}+',')>0 ", keyword.Replace("'", "''"), fieldname.Replace("'", "''"));
                if (i == keywords.Length - 1)
                {
                    charIndexStr += ")";
                }
                else
                {
                    charIndexStr += " or ";
                }
            }
            return charIndexStr;
        }


        //public static void ClearContainter(Coolite.Ext.Web.Container container)
        //{
        //    foreach (System.Web.UI.Control item in container.Controls)
        //    {

        //        ItemClear(item);
        //    }
        //}

        //public static void ItemClear(System.Web.UI.Control control)
        //{
        //    if (control.HasControls())
        //    {
        //        foreach (System.Web.UI.Control ctl in control.Controls)
        //        {
        //            if (control.GetType().Name == "ComboBox")
        //            {
        //                Coolite.Ext.Web.ComboBox cb = new Coolite.Ext.Web.ComboBox();
        //                cb = (Coolite.Ext.Web.ComboBox)ctl.FindControl(control.ID);
        //                cb.SelectedItem.Value = "";
        //                continue;
        //            }
        //            ItemClear(ctl);

        //        }

        //    }
        //    else
        //    {
        //        if (control.GetType().Name == "TextField")
        //        {
        //            Coolite.Ext.Web.TextField tb = new Coolite.Ext.Web.TextField();
        //            tb = (Coolite.Ext.Web.TextField)control.FindControl(control.ID);
        //            tb.Text = "";
        //        }
        //        if (control.GetType().Name == "TextArea")
        //        {
        //            Coolite.Ext.Web.TextArea ta = new Coolite.Ext.Web.TextArea();
        //            ta = (Coolite.Ext.Web.TextArea)control.FindControl(control.ID);
        //            ta.Text = "";
        //        }



        //        if (control.GetType().Name == "DateField")
        //        {
        //            Coolite.Ext.Web.DateField df = new Coolite.Ext.Web.DateField();
        //            df = (Coolite.Ext.Web.DateField)control.FindControl(control.ID);
        //            df.Value = "";
        //        }
        //    }
        //}
    }
}
